package com.zd.commodity.userMng.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.opensymphony.xwork2.ActionContext;
import com.zd.commodity.action.BaseAction;
import com.zd.commodity.userMng.biz.IMmenuBiz;
import com.zd.commodity.userMng.biz.IRoleBiz;
import com.zd.commodity.userMng.entity.Mmenu;
import com.zd.commodity.userMng.entity.User;

public class MmenuAction extends BaseAction<Mmenu>{


	private IMmenuBiz mmenuBiz;
	private IRoleBiz roleBiz;
	private String uid;
	private Long rid;
	private User user=(User)request.getSession().getAttribute("user");
	public void setMmenuBiz(IMmenuBiz mmenuBiz) {
		this.mmenuBiz = mmenuBiz;
		setBaseBiz(mmenuBiz);
	}
	
	public void getRoleMenuListToUpdate() throws IOException{
		List<Mmenu> roleMenuListToUpdate = mmenuBiz.getRoleMenuListToUpdate(rid);//user.getRole().getRid()
		SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Mmenu.class, "text", "href","nodes","state","tags");
		String jsonString = JSON.toJSONString(roleMenuListToUpdate,filter);
		super.responseWriter(jsonString);
	}

	public void getMmenu() throws IOException{
		Mmenu mmenu = mmenuBiz.get(super.getId());
		SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Mmenu.class, "text", "href","nodes");
		String jsonString = JSON.toJSONString(mmenu,filter);
		super.responseWriter(jsonString);
	}
	
	public void getRoleMenu() throws IOException{

		/*Long rid=user.getRole().getRid();*/
		List<Mmenu> roleMenu = mmenuBiz.getRoleMenu(1L);
		String jsonString = JSON.toJSONString(roleMenu);
		super.responseWriter(jsonString);
	}
	
	@Override
	public String toAdd(){
		String menus=request.getParameter("menus");
		if(menus.equals("subMenus")){
			int param=0;
			List<Mmenu> mains=mmenuBiz.getList(super.getT1(), super.getT2(), (Object)Integer.toString(param));
			ActionContext.getContext().put("mains", mains);
		}
		request.setAttribute("menus", menus);
		return "toAdd";
	}
	
	
	public String getSubMenu()
	{		
		Mmenu submenu =mmenuBiz.get(super.getId());
		Mmenu mainmenu=mmenuBiz.get(submenu.getParentId());
		
		request.setAttribute("params", super.getParams());
		ActionContext.getContext().put("m", mainmenu);
		ActionContext.getContext().put("t", submenu);
		ActionContext.getContext().put("method", "update");
		return "form";
	}
	
	public void setRoleBiz(IRoleBiz roleBiz) {
		this.roleBiz = roleBiz;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public void setRid(Long rid) {
		this.rid = rid;
	}

}
